<?php

/*
*
* BEGIN full_scorecard_ui.php
*
*
* Dazzler Full ScoreCard Uis
* @Description: The class creates all the UIs for the full score card tab for live match ui
* @Authors: Salman, Wits University
* @Copyright: © Wits University 2011 - All Rights Reserved
* @License: BSD License (http://www.linfo.org/bsdlicense.html)
*
*
* BEGIN CHANGELOG
* Developers working on thee files are REQUIRED to maintain this CHANGELOG.
* Please ensure you follow the Changelog Guidelines: http://code.google.com/p/dazzler/wiki/DazzlerDevelopGuideline?ts=1315476570&updated=DazzlerDevelopGuideline
* !==================================================================================================================================================================
*
* VERSION 1.0.0
*	+ File Created, Salman, 13 October 2011
*
* ==================================================================================================================================================================!
* END CHANGELOG
*/


/*******************************************************************************************************************************************************************
*
* BEGIN Direct Call Check
*
/******************************************************************************************************************************************************************/
global $doNotCallThisScriptDirectly;
if ($doNotCallThisScriptDirectly != 1)
{
	/* Read the ini language file */
	$ini_lang_array = array();
	$clientlang = explode(",", strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE']));
	$lang_ini_array = (file_exists("lang/" . trim($clientlang[0]) . ".ini")) ? parse_ini_file("lang/" . trim($clientlang[0]) . ".ini", true) : parse_ini_file("lang/en.ini", true);
	die ($lang_ini_array['ERRORS']['doNotCallThisScriptDirectly']);
}
/*******************************************************************************************************************************************************************
*
* END Direct Call Check
*
/******************************************************************************************************************************************************************/

class full_scorecard_ui extends baseClass
{

    private $htmlelementsbuilder;
    private $gameID=null;
    private $innings=null;
    private $parsesiteconfig;
    private $StatsAdminObj;
    public function  __construct($gameID,$innings)
    {
    $this->htmlelementsbuilder = new htmlelementsbuilder();
    $this->parsesiteconfig = new parsesiteconfig();
    parent::parseLanguageErrorLogFile();
    if (isset($gameID))
    {
        $this->gameID = $gameID;
    }
    if (isset($innings))
    {
        //$this->innings = $innings;
        $this->innings = '2';
    }
    $this->StatsAdminObj = new StatsAdmin();

    }

       private function checkNum($num){
  return ($num%2) ? TRUE : FALSE;
}

    private function buildFullBattingScoreCard()
    {
      $battingStatsArray =  $this->StatsAdminObj->getFullBattingScoreCard($this->gameID, 1);


      $teamsArrayForFirstInninngs =$this->StatsAdminObj->getBattingAndBowlingTeamsForInnings($this->gameID, "1");
        $teamsArrayForSecondInninngs =$this->StatsAdminObj->getBattingAndBowlingTeamsForInnings($this->gameID, "2");
 $batsmanStillToBatArray =$this->StatsAdminObj->stillToBat($this->gameID, 1);
// print_r($batsmanStillToBatArray);
        ///  print_r($battingStatsArray);
 //   print_r("<BR/>");
//    print_r($teamsArrayForSecondInninngs);
        $fullBattingScoreCard = "<h4>First Innings Batting Scorecard - ".$teamsArrayForFirstInninngs['batting']['TEAM_POOL_NAME']."</h4>";
  

           $fullBattingScoreCard.= '<table width="100%" cellpadding="5" cellspacing="0">
            <tr>
                    <th align="left">Batsman</th>
                    <th width="85">Runs</th>
                    <th width="85">Balls</th>
                    <th width="85">Strike Rate</th>
                    <th width="85">Fours</th>
                    <th width="85">Sixes</th>
            </tr>';
           foreach ($battingStatsArray as $key => $batsman) {
  if($this->checkNum($key) === FALSE)
  {
   $fullBattingScoreCard.="<tr bgcolor='#F3F3F3'>";

     }
  else
  {
    $fullBattingScoreCard .= "<tr>";
  }
  $fullBattingScoreCard .="<td>".$batsman['PLAYER_POOL_FNAME']." ". $batsman['PLAYER_POOL_LNAME'];
  if ($batsman['dismissed'] == "1")
  {
$fullBattingScoreCard .= "&nbsp;&nbsp;&nbsp; ".$batsman['BALLBYBALL_DIS_TYP']."</td>";
  }
  else
  {
 $fullBattingScoreCard .= "</td>";
  }
   $fullBattingScoreCard .=     "<td align='center'>".$batsman['runs']."</td>
        <td align='center'>".$batsman['balls']."</td>
           <td align='center'>".round($batsman['SR'], 2)."</td>
                      <td align='center'>".$batsman['Fours']."</td>
                                <td align='center'>".$batsman['Sixes']."</td>
        <tr/>";

           }

           if (!empty($batsmanStillToBatArray))
           {
                $fullBattingScoreCard.="<tr bgcolor='#F3F3F3'><td>Still to Bat</td><td></td><td></td><td></td><td></td><td></td></tr>";
               foreach ($batsmanStillToBatArray as $key => $batsmanStillToBat) {
                 if($this->checkNum($key) === FALSE)
                  {
                   $fullBattingScoreCard.="<tr bgcolor='#F3F3F3'>";

                     }
                  else
                  {
                    $fullBattingScoreCard .= "<tr>";
                  }
                    $fullBattingScoreCard .="<td>".$batsmanStillToBat['name']." ". $batsmanStillToBat['surname']."</td><tr/>";
               }
           }

           if (empty($battingStatsArray))
           {
             $fullBattingScoreCard .= "<tr><td>No batsman have currently batted</td></tr><tr><td>Wait a few minutes for the match to actually start.</td></tr>";
           }
          $fullBattingScoreCard.= "</table>";

          if ($this->innings == "2")
          {
             $fullBattingScoreCard .= "<h4>Second Innings Batting Scorecard - ".$teamsArrayForSecondInninngs['batting']['TEAM_POOL_NAME']."</h4>";
          
                        $fullBattingScoreCard.= '<table width="100%" cellpadding="5" cellspacing="0">
            <tr>
                    <th align="left">Batsman</th>
                    <th width="85">Runs</th>
                    <th width="85">Balls</th>
                    <th width="85">Strike Rate</th>
                    <th width="85">Fours</th>
                    <th width="85">Sixes</th>
            </tr>';
             $SecondInningsbattingStatsArray =  $this->StatsAdminObj->getFullBattingScoreCard($this->gameID, 2);
     
             foreach ($SecondInningsbattingStatsArray as $key => $batsman) {
                    
  if($this->checkNum($key) === FALSE)
  {
   $fullBattingScoreCard.="<tr bgcolor='#F3F3F3'>";

     }
  else
  {
    $fullBattingScoreCard .= "<tr>";
  }
  $fullBattingScoreCard .="<td>".$batsman['PLAYER_POOL_FNAME']." ". $batsman['PLAYER_POOL_LNAME'];
  if ($batsman['dismissed'] == "1")
  {
$fullBattingScoreCard .= "&nbsp;&nbsp;&nbsp; ".$batsman['BALLBYBALL_DIS_TYP']."</td>";
  }
  else
  {
 $fullBattingScoreCard .= "</td>";
  }
   $fullBattingScoreCard .=     "<td align='center'>".$batsman['runs']."</td>
        <td align='center'>".$batsman['balls']."</td>
           <td align='center'>".round($batsman['SR'], 2)."</td>
                      <td align='center'>".$batsman['Fours']."</td>
                                <td align='center'>".$batsman['Sixes']."</td>
        <tr/>";

           }
            $secondInningsBatsmanStillToBatArray =$this->StatsAdminObj->stillToBat($this->gameID, 2);
           if (!empty($secondInningsBatsmanStillToBatArray))
           {
                $fullBattingScoreCard.="<tr bgcolor='#F3F3F3'><td>Still to Bat</td><td></td><td></td><td></td><td></td><td></td></tr>";
               foreach ($secondInningsBatsmanStillToBatArray as $key => $batsmanStillToBat) {
                 if($this->checkNum($key) === FALSE)
                  {
                   $fullBattingScoreCard.="<tr bgcolor='#F3F3F3'>";

                     }
                  else
                  {
                    $fullBattingScoreCard .= "<tr>";
                  }
                    $fullBattingScoreCard .="<td>".$batsmanStillToBat['name']." ". $batsmanStillToBat['surname']."</td><tr/>";
               }
           }
             $fullBattingScoreCard.= "</table>";
          }
          
        return $fullBattingScoreCard;


    }


    private function buildBowlingScoreCard()
    {
        
        $bowlingScoreCard = NULL;
        $bowlingStatsArray = $this->StatsAdminObj->getFullBowlingScoreCard($this->gameID, 1);
      //  print_r($bowlingStatsArray);
      $teamsArrayForFirstInninngs =$this->StatsAdminObj->getBattingAndBowlingTeamsForInnings($this->gameID, "1");
        $teamsArrayForSecondInninngs =$this->StatsAdminObj->getBattingAndBowlingTeamsForInnings($this->gameID, "2");

        $bowlingScoreCard = "<h4>First Innings Bowling Scorecard - ".$teamsArrayForFirstInninngs['bowling']['TEAM_POOL_NAME']."</h4>";
        
        //print_r($bowlingStatsArray);
       $bowlingScoreCard.= '<table width="100%" cellpadding="5" cellspacing="0">
            <tr>
                    <th align="left">Bowler</th>
                    <th width="85">Runs</th>
                    <th width="85">Wickets</th>
                    <th width="85">Balls</th>
                    <th width="85">Overs</th>
                    <th width="85">Economy</th>
                    <th width="250">List of Extras</th>
            </tr>';
       foreach ($bowlingStatsArray as $key => $bowler) {
  if($this->checkNum($key) === FALSE)
  {
   $bowlingScoreCard.="<tr bgcolor='#F3F3F3'>";

     }
  else
  {
    $bowlingScoreCard .= "<tr>";
  }
  $bowlingScoreCard .="<td>".$bowler['player first name']." ". $bowler['player last name']."</td>
        <td align='center'>".$bowler['runs']."</td>
        <td align='center'>".$bowler['wickets']."</td>
           <td align='center'>".$bowler['balls']."</td>
          <td align='center'>".$bowler['over']."</td>
        <td align='center'>".round($bowler['economy'], 2)."</td>
        ";
  $bowlingScoreCard .= "<td align='center'>";
  $extraTypesWithOutNulls = array_filter($bowler['extra type'], 'strlen');
  $extraTypesCountArray = array_count_values($extraTypesWithOutNulls);

  foreach ($extraTypesCountArray as $extraType =>$value)
  {

       $bowlingScoreCard .= "(".$value." ".$extraType.")&nbsp; ";

 
  }
  $bowlingScoreCard .= "</td>";
           }
       $bowlingScoreCard.= "</table>";

     if ($this->innings == "2")
      {
 $bowlingScoreCard .= "<h4>Second Innings Bowling Scorecard - ".$teamsArrayForSecondInninngs['bowling']['TEAM_POOL_NAME']."</h4>";
       $secondInningsBowlingStatsArray = $this->StatsAdminObj->getFullBowlingScoreCard($this->gameID, 2);
             $bowlingScoreCard.= '<table width="100%" cellpadding="5" cellspacing="0">
            <tr>
                    <th align="left">Bowler</th>
                    <th width="85">Runs</th>
                    <th width="85">Wickets</th>
                    <th width="85">Balls</th>
                    <th width="85">Overs</th>
                    <th width="85">Economy</th>
                    <th width="250">List of Extras</th>
            </tr>';
             foreach ($secondInningsBowlingStatsArray as $key => $bowler) {
  if($this->checkNum($key) === FALSE)
  {
   $bowlingScoreCard.="<tr bgcolor='#F3F3F3'>";

     }
  else
  {
    $bowlingScoreCard .= "<tr>";
  }
  $bowlingScoreCard .="<td>".$bowler['player first name']." ". $bowler['player last name']."</td>
        <td align='center'>".$bowler['runs']."</td>
        <td align='center'>".$bowler['wickets']."</td>
           <td align='center'>".$bowler['balls']."</td>
          <td align='center'>".$bowler['over']."</td>
        <td align='center'>".round($bowler['economy'], 2)."</td>
        ";
  $bowlingScoreCard .= "<td align='center'>";
  $extraTypesWithOutNulls = array_filter($bowler['extra type'], 'strlen');
  $extraTypesCountArray = array_count_values($extraTypesWithOutNulls);

  foreach ($extraTypesCountArray as $extraType =>$value)
  {

       $bowlingScoreCard .= "(".$value." ".$extraType.")&nbsp; ";


  }
  $bowlingScoreCard .= "</td>";
           }
       $bowlingScoreCard.= "</table>";
 
     }
        return $bowlingScoreCard;
    }

    private function buildFullFallofWickets()
    {
        $fallofWicketsArray = $this->StatsAdminObj->getFallOfWickets($this->gameID, 1);
      $teamsArrayForFirstInninngs =$this->StatsAdminObj->getBattingAndBowlingTeamsForInnings($this->gameID, "1");
    $teamsArrayForSecondInninngs =$this->StatsAdminObj->getBattingAndBowlingTeamsForInnings($this->gameID, "2");

    
// print_r($batsmanStillToBatArray);
        ///  print_r($battingStatsArray);
 //   print_r("<BR/>");
//    print_r($teamsArrayForSecondInninngs);
 
        $fallOFWicketsCard = "<h4>First Innings Fall of Wickets Score Card - ".$teamsArrayForFirstInninngs['batting']['TEAM_POOL_NAME']."</h4>";
       $fallOFWicketsCard.= '<table width="100%" cellpadding="5" cellspacing="0">
            <tr>
                    <th width="30">Dismissal</th>
                    <th align="left">Batsman</th>
                    <th width="85">Runs</th>
                    <th width="85">Wickets</th>
                    <th width="85">Over</th>
      
            </tr>';
       foreach ($fallofWicketsArray as $key => $wickets) {
  if($this->checkNum($key) === FALSE)
  {
   $fallOFWicketsCard.="<tr bgcolor='#F3F3F3'>";

     }
  else
  {
    $fallOFWicketsCard .= "<tr>";
  }
  $fallOFWicketsCard .="<td>".$wickets['dismissals']."</td>
      <td>".$wickets['first_name']." ". $wickets['last_name']."</td>
        <td align='center'>".$wickets['runs']."</td>
        <td align='center'>".($key+1)."</td>
           <td align='center'>".$wickets['over']."</td>
        ";

           }
       $fallOFWicketsCard.= "</table>";
       if ($this->innings == "2")
       {
         $secondInningsFallofWicketsArray = $this->StatsAdminObj->getFallOfWickets($this->gameID, 2);
          $fallOFWicketsCard .= "<h4>Second Innings Fall of Wickets Score Card - ".$teamsArrayForSecondInninngs['batting']['TEAM_POOL_NAME']."</h4>";
       $fallOFWicketsCard.= '<table width="100%" cellpadding="5" cellspacing="0">
            <tr>
                    <th width="30">Dismissal</th>
                    <th align="left">Batsman</th>
                    <th width="85">Runs</th>
                    <th width="85">Wickets</th>
                    <th width="85">Over</th>

            </tr>';
       foreach ($secondInningsFallofWicketsArray as $key => $wickets) {
  if($this->checkNum($key) === FALSE)
  {
   $fallOFWicketsCard.="<tr bgcolor='#F3F3F3'>";

     }
  else
  {
    $fallOFWicketsCard .= "<tr>";
  }
  $fallOFWicketsCard .="<td>".$wickets['dismissals']."</td>
      <td>".$wickets['first_name']." ". $wickets['last_name']."</td>
        <td align='center'>".$wickets['runs']."</td>
        <td align='center'>".($key+1)."</td>
           <td align='center'>".$wickets['over']."</td>
        ";

           }
       $fallOFWicketsCard.= "</table>";
       }
       return $fallOFWicketsCard;
        
    }

    public function showFallofWickets()
    {
        return $this->buildFullFallofWickets();
    }

    public function showFullBowlingScoreCard()
    {
        return $this->buildBowlingScoreCard();
    }

    public function showFullBattingScoreCard()
    {
        return $this->buildFullBattingScoreCard();
    }






}